#include "../../Headers/DataAccessors/typeofroomdataaccessor.h"

#include <QSqlQuery>

TypeOfRoomDataAccessor::TypeOfRoomDataAccessor(QString databaseName):
    DB(databaseName)
{
}

QSqlTableModel * TypeOfRoomDataAccessor::getTypesOfRoom()
{
    QSqlTableModel * model = new QSqlTableModel();
    model->setTable("typeOfRoom");
    model->select();
    return model;
}

bool TypeOfRoomDataAccessor::setTypeOfRoom(TypeOfRoom typeOfRoom)
{
    QString values = "insert into typeOfRoom (name, count, cost) values (\""
            + typeOfRoom.getName() + "\", " +
            QString::number(typeOfRoom.getCount()) + ", " +
            QString::number(typeOfRoom.getCost()) + ")";
    QSqlQuery q(db);
    if (q.exec(values))
    {
        qDebug("all ok");
        return true;
    }
    else
    {
        qDebug("something wrong");
        qDebug(values.toAscii());
        return false;
    }
}

TypeOfRoom * TypeOfRoomDataAccessor::getTypeOfRoom(int id)
{
    QSqlQuery query(db);
    if (query.exec("select id, name, count, cost from typeOfRoom where id = " + QString::number(id)))
    {
        while(query.next())
        {
            TypeOfRoom *typeOFRoom = new TypeOfRoom(
                        query.value(0).toInt(),//id
                        query.value(1).toString(),//name
                        query.value(2).toInt(),//count
                        query.value(3).toDouble()//cost
                        );
            return typeOFRoom;
        }
    }

    return NULL;
}

bool TypeOfRoomDataAccessor::deleteTypeOfRoom(TypeOfRoom typeOfRoom)
{
    QSqlQuery query(db);
    if (query.exec("delete from typeOfRoom where id=" + QString::number(typeOfRoom.getId())))
    {
        return true;
    }
    else
        return false;
}

bool TypeOfRoomDataAccessor::editTypeOfRoom(TypeOfRoom typeOfRoom)
{
    QSqlQuery query(db);
    if (query.exec("update typeOfRoom set name=\"" + typeOfRoom.getName() +
                   "\", count=" + QString::number(typeOfRoom.getCount()) +
                   ", cost=" + QString::number(typeOfRoom.getCost()) +
                   " where id=" + QString::number(typeOfRoom.getId())))
        return true;
    else
        return false;
}
